package com.service.impl;

import java.util.List;

import org.apache.ibatis.session.SqlSession;

import com.dao.UserDao;
import com.entity.User;
import com.service.UserService;
import com.sqlSessionFactory.SqlSessionFactoryUtil;

public class UserserviceImpl implements UserService {

	private static final Exception Exception = null;

	@Override
	public boolean registUser(User user) throws java.lang.Exception {
		SqlSession sqlSession = SqlSessionFactoryUtil.getSqlSession();
		UserDao userDao = sqlSession.getMapper(UserDao.class);
		try{
			int recoder = userDao.registUser(user);
			sqlSession.commit();
			sqlSession.close();
			if(recoder>0){
				return true;
			}else{
				return false;
			}
		}catch(Exception e){
			throw Exception;
		}
	}

	@Override
	public boolean checkName(String username) {
		SqlSession sqlSession = SqlSessionFactoryUtil.getSqlSession();
		UserDao userDao = sqlSession.getMapper(UserDao.class);
		List<User> listUser = userDao.checkName(username);
		System.out.println(listUser.size());
		//sqlSession.commit();
		sqlSession.close();
		if(listUser.size()>0) {
			return false;
		}else {
			return true;
		}
	}

	@Override
	public User login(User user) {
		SqlSession sqlSession = SqlSessionFactoryUtil.getSqlSession();
		UserDao userDao = sqlSession.getMapper(UserDao.class);
		User us = userDao.login(user);
		//System.out.println(us.toString());
		//sqlSession.commit();
		sqlSession.close();
		return us;
	}
}
